package com.example.ywr.controller;

import com.example.ywr.common.PageParam;
import com.example.ywr.common.PageResult;
import com.example.ywr.common.Result;
import com.example.ywr.dto.CategoryRequest;
import com.example.ywr.entity.Category;
import com.example.ywr.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类控制器
 */
@Api(tags = "商品分类管理")
@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation("添加分类")
    @PostMapping
    public Result<Category> add(@Validated @RequestBody CategoryRequest request) {
        Category category = new Category();
        BeanUtils.copyProperties(request, category);
        Category addedCategory = categoryService.add(category);
        return Result.success(addedCategory);
    }

    @ApiOperation("更新分类")
    @PutMapping("/{id}")
    public Result<Category> update(@PathVariable Long id, @Validated @RequestBody CategoryRequest request) {
        Category category = new Category();
        BeanUtils.copyProperties(request, category);
        category.setId(id);
        Category updatedCategory = categoryService.update(category);
        if (updatedCategory == null) {
            return Result.failed("分类不存在");
        }
        return Result.success(updatedCategory);
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = categoryService.delete(id);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("删除失败");
    }

    @ApiOperation("批量删除分类")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        boolean success = categoryService.batchDelete(ids);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("批量删除失败");
    }

    @ApiOperation("获取分类详情")
    @GetMapping("/{id}")
    public Result<Category> getById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        if (category == null) {
            return Result.failed("分类不存在");
        }
        return Result.success(category);
    }

    @ApiOperation("分页查询分类列表")
    @GetMapping
    public Result<PageResult<Category>> list(PageParam pageParam) {
        PageResult<Category> pageResult = categoryService.list(pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("根据父分类ID查询子分类")
    @GetMapping("/parent/{parentId}")
    public Result<List<Category>> listByParentId(@PathVariable Long parentId) {
        List<Category> categories = categoryService.listByParentId(parentId);
        return Result.success(categories);
    }

    @ApiOperation("根据层级查询分类")
    @GetMapping("/level/{level}")
    public Result<List<Category>> listByLevel(@PathVariable Integer level) {
        List<Category> categories = categoryService.listByLevel(level);
        return Result.success(categories);
    }

    @ApiOperation("根据名称模糊查询")
    @GetMapping("/search")
    public Result<PageResult<Category>> listByName(@RequestParam String name, PageParam pageParam) {
        PageResult<Category> pageResult = categoryService.listByName(name, pageParam);
        return Result.success(pageResult);
    }

    @ApiOperation("更新分类状态")
    @PutMapping("/{id}/status/{status}")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean success = categoryService.updateStatus(id, status);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("更新状态失败");
    }

    @ApiOperation("批量更新分类状态")
    @PutMapping("/batch/status/{status}")
    public Result<Boolean> batchUpdateStatus(@RequestBody List<Long> ids, @PathVariable Integer status) {
        boolean success = categoryService.batchUpdateStatus(ids, status);
        if (success) {
            return Result.success(true);
        }
        return Result.failed("批量更新状态失败");
    }
}